/*****************************************************************
 *   Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 ****************************************************************/
package org.epiphron;

import org.epiphron.ValidityReport.Report;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.reasoner.InconsistentOntologyException;

/**
 * Simple helper implementation of {@link SemanticValidator}.
 * This one simply delegates validation to two helper interfaces.
 * First, it adds objects to ontology in form of instances.
 * Then, validates ontology full with data. 
 */
public class BasicSemanticValidator implements SemanticValidator {
	
	/**
	 * Converter of beans to RDF
	 */
	BeanConverter converter;
	
	/**
	 * Validator of full ontology (schema plus instances)
	 */
	ModelValidator validator;
	
	/**
	 * Creates a {@link BasicSemanticValidator}
	 * @param _converter Converter of beans to RDF
	 * @param _validator Validator of full ontology (schema plus instances)
	 */
	protected BasicSemanticValidator(BeanConverter _converter, ModelValidator _validator) {
		converter = _converter;
		validator = _validator;
	}
	
	public ValidityReport validate(OWLOntology ontology, Object... beans) throws EpiphronException {
		return validate(ValidationLevel.UNA_CWA, ontology, beans);
	}
	
	public ValidityReport validateAndInfer(OWLOntology ontology, Object... beans) throws EpiphronException {
		return validate(ValidationLevel.INFER, ontology, beans);
	}

	public ValidityReport validate(ValidationLevel level, OWLOntology ontology, Object... beans) throws EpiphronException {
		OWLOntology workingCopy;
		try {
			workingCopy = OWLManager.createOWLOntologyManager().createOntology(
					ontology.getAxioms(),
					ontology.getOntologyID().getOntologyIRI());
		} catch (OWLOntologyCreationException e) {
			throw new EpiphronException("Cannot create ontology working copy", e);
		}
		
		ConversionMatching matching = converter.convert(workingCopy, beans);		
		SimpleValidityReport report = new SimpleValidityReport();
		
		if (level.shouldInfer()) {
			try {
				report.addReports(validator.infer(workingCopy, matching, level.shouldUseUNA()));
			}
			catch (InconsistentOntologyException ex) {
				report.addReport(new Report(ReportType.ERROR, ex.getLocalizedMessage()));
				return report;
			}
		}
		
		if (level.shouldUseCWA()) {
			converter.closeWorld(workingCopy);
		}
		
//		try {
//			ontology.getOWLOntologyManager().saveOntology(workingCopy, System.out);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		report.addReport(validator.validate(workingCopy, level.shouldUseUNA()));
		
		return report;
	}

}
